<?php

namespace common\models\shop;

use Yii;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%shop_activity}}".
 *
 * @property int $id
 * @property string $name 活动名称
 * @property string $description 活动描述
 * @property int $begin_time 开始时间
 * @property int $end_time 截止时间
 * @property string $tag 活动标签
 * @property int $map_id
 * @property string $map_data
 * @property int $listorder
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class ShopActivity extends \common\db\ActiveRecord {

    const STATUS_DEFAULT = 0;
    const STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_activity}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['begin_time', 'end_time', 'map_id', 'listorder', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['description', 'map_data'], 'string', 'max' => 255],
            [['tag'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '活动名称',
            'description' => '活动描述',
            'begin_time' => '开始时间',
            'end_time' => '截止时间',
            'tag' => '活动标签',
            'map_id' => 'Map ID',
            'map_data' => 'Map Data',
            'listorder' => 'Listorder',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getTheme() {
        return $this->hasMany(ShopActivityTheme::className(), ['activity_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getThemeData() {
        return ArrayHelper::map($this->theme, 'name', 'data');
    }

    /**
     * {@inheritdoc}
     */
    public function getProducts() {
        return $this->hasMany(ShopActivityProduct::className(), ['activity_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'name',
            'description',
            'begin_time',
            'end_time',
            'link',
            'theme' => 'themeData',
            'products' => function() {
                return $this->getProducts()->limit(3)->all();
            },
        ];
    }

}
